package com.icexxx.icemock.mock;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;

public class IntMock implements IceMock {

	@Override
	public String mock(Integer minVal, Integer maxVal, Integer minWidth, Integer maxWidth, String enums,
			String key) {
		if (maxVal == null) {
			Integer width = null;
			if (minWidth == null && maxWidth == null) {
				width = 5;
			} else {
				if (minWidth == null) {
					width = maxWidth;
				} else if (maxWidth == null) {
					width = minWidth;
				} else {
					width = RandomUtil.randomInt(minWidth, maxWidth + 1);
				}
			}
			String randomInt = RandomUtil.randomNumbers(width);
			return randomInt;
		} else {
			if(minVal == null) {
				minVal = 0;
			}
			int randomInt = RandomUtil.randomInt(minVal, maxVal + 1);
			String str = String.valueOf(randomInt);
			int len = str.length();
			if (minWidth == null) {
				minWidth = 0;
			}
			if (maxWidth == null) {
				maxWidth = 5;
			}
			if (len < minWidth) {
				String fillBefore = StrUtil.fillBefore(str, '0', minWidth);
				return fillBefore;
			}
			if (len > maxWidth) {
				String subPre = StrUtil.subPre(str, maxWidth);
				return subPre;
			}
			return str;
		}
	}

}
